from gevent import monkey
import gevent
import random
import time

# 有耗时操作时需要
monkey.patch_all()  # 将程序中用到的耗时操作的代码，换为gevent中自己实现的模块


def coroutine_work(coroutine_name):
    for i in range(10):
        # print(coroutine_name, i)
        print("--------1--------")
        time.sleep(random.random())


def coroutine_work2(coroutine_name):
    for i in range(10):
        # print(coroutine_name, i)
        print("--------2--------")
        time.sleep(random.random())


# 方式1
# # 1. 创建
# g1 = gevent.spawn(coroutine_work, "work1")
# g2 = gevent.spawn(coroutine_work2, "work2")
# # 2. 等待协程结束
# g1.join()
# g2.join()

# 方式2：创建&等待合并操作
gevent.joinall([
    gevent.spawn(coroutine_work, "work1"),
    gevent.spawn(coroutine_work2, "work2")
])
